package com.leoman.controller;

import com.leoman.core.Constant;
import com.leoman.entity.*;
import com.leoman.entity.vo.StatData;
import com.leoman.entity.vo.StatUserData;
import com.leoman.service.*;
import com.leoman.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公共方法控制层
 * Created by 涂奕恒 on 2016/10/18.
 */
@Controller
@RequestMapping(value = "common")
public class CommonsController {

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private RegionService regionService;

    @Autowired
    private VideoTypeService videoTypeService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoRecordService videoRecordService;

    @Autowired
    private RecordInfoService recordInfoService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private CompanyUserService companyUserService;

    @Autowired
    private UserPraiseService userPraiseService;

    /**
     * 省份列表
     */
    @RequestMapping("/provinceList")
    @ResponseBody
    public List<Province> provinceList() {
        return provinceService.findAll();
    }

    /**
     * 城市列表
     */
    @RequestMapping("/cityList")
    @ResponseBody
    public List<City> cityList(Integer provinceId) {
        return cityService.findListByProvinceId(provinceId);
    }

    /**
     * 区域列表
     */
    @RequestMapping("/areaList")
    @ResponseBody
    public List<Region> areaList(Integer cityId) {
        return regionService.findListByCityId(cityId);
    }

    /**
     * 视频分类列表
     */
    @RequestMapping("/videoTypeList")
    @ResponseBody
    public List<VideoType> videoTypeList() {
        return videoTypeService.findAll();
    }

    /**
     * 视频列表
     */
    @RequestMapping("/videoList")
    @ResponseBody
    public List<VideoRecord> videoList(Integer videoTypeId) {
        return videoService.findListByVideoTypeIdDefault(videoTypeId);
    }

    /**
     * 视频列表
     */
    @RequestMapping("/myVideoList")
    @ResponseBody
    public List<Video> myVideoList(Integer companyUserId) {
        return videoService.findListByCompanyUserId(companyUserId);
    }

    /**
     * 首页获取统计数据
     */
    @RequestMapping("/statData")
    @ResponseBody
    public StatData statData(Integer day) {
        return recordInfoService.getDataByDay(day);
    }

    /**
     * 首页获取统计数据
     */
    @RequestMapping("/statVideoData")
    @ResponseBody
    public StatUserData statVideoData(HttpServletRequest request, Integer day, Integer videoId) {
        CompanyUser companyUser = (CompanyUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);
        return videoService.iFindInfoByCompanyUserIdAndIndex(companyUser.getId(), day, videoId);
    }

    /**
     * 首页获取用户统计数据
     */
    @RequestMapping("/statUserData")
    @ResponseBody
    public StatUserData statUserData(Integer companyUserId) {
        return appUserService.getStatUserData(companyUserId);
    }

    /**
     * 首页获取统计数据
     */
    @RequestMapping("/getStatInfoByCompanyUserId")
    @ResponseBody
    public StatUserData getStatInfoByCompanyUserId(HttpServletRequest request) {
        CompanyUser companyUser = (CompanyUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);
        return companyUserService.getStatInfoByCompanyUserId(companyUser.getId());
    }

    @RequestMapping("/shareVideo")
    public String shareVideo(ModelMap model, Integer videoId, Integer type) {
        Video video;

        // type为空或者为1时，代表是用户端分享，否则是企业端分享
        if (null == type || type == 1) {
            VideoRecord videoRecord = videoRecordService.getById(videoId);
            video = videoRecord.getVideo();
            model.addAttribute("videoRecord", videoRecord);
        } else {
            video = videoService.getById(videoId);
        }

        model.addAttribute("video", video);
        model.addAttribute("duration", QiNiuUtils.getVideoTime(video.getDuration()));
        model.addAttribute("praiseNum", userPraiseService.findListByVideoIdAndType(video.getId(), Constant.USER_PRAISE).size());
        model.addAttribute("treadNum", userPraiseService.findListByVideoIdAndType(video.getId(), Constant.USER_TREAD).size());

        if (null == type || type == 1) {
            return "backend/视频分享";
        } else {
            return "company/视频分享";
        }
    }
}
